import axios from 'axios'

// https://www.kancloud.cn/yunye/axios/234845 详细配置
let notice = null
axios.interceptors.request.use(
  config => {
    let userToken = localStorage.getItem('userToken')
    if (userToken !== null) {
      config.headers['token'] = userToken
      config.headers['Content-Type'] = "application/json;charset=UTF-8"
    }
    
    config.params = {
      _t: Date.parse(new Date()) / 1000,
      ...config.params
    }
    return config
  }, error => {
    return Promise.reject(error)
  }
)
axios.interceptors.response.use(function (response) {
  // token 已过期，重定向到登录页面
  let code = response.data.code  
  return response.data  
}, error => {
  if (error.response) {
    let message = error.response.data.msg
    switch (error.response.status) {
      case 401:
       
        break
      case 403:
       
        if (!notice) {
         
        }
        router.replace({
          path: '/login',
          query: {redirect: router.currentRoute.fullPath}
        })
        break
      case 500:
       
        break
      case 504:
      
        break
      default:
       
    }
  }
  return Promise.reject(error)
})
export default axios
